﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;

namespace Restaurant.DAO
{
    public class HoaDonDB
    {
        private SqlConnection con = KetNoiDB.TaoKetNoi();

        public DataTable LoadHoaDon()
        {
            con.Open();
            string strcmd = "dbo.spLoadHoaDon";
            SqlCommand cmd = new SqlCommand(strcmd, con);
            cmd.CommandType = CommandType.StoredProcedure;
            SqlDataAdapter adapt = new SqlDataAdapter(cmd);
            DataTable dbtb = new DataTable();
            adapt.Fill(dbtb);
            con.Close();
            return dbtb;
        }

        public DataTable LoadHoaDonTheoNgayNH(int MaNH, DateTime Ngay)
        {
            con.Open();
            string strcmd = "dbo.spLoadHoaDonTheoNgayNH";
            SqlCommand cmd = new SqlCommand(strcmd, con);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add(new SqlParameter("@maNH", MaNH));
            cmd.Parameters.Add(new SqlParameter("@ngayLapHD", Ngay));
            SqlDataAdapter adapt = new SqlDataAdapter(cmd);
            DataTable dbtb = new DataTable();
            adapt.Fill(dbtb);
            con.Close();
            return dbtb;
        }

        public bool SuaHoaDon(int maHD, int maKH, int maNH, DateTime ngayLapHD, int thanhTien)
        {
            try
            {
                con.Open();
                string strcmd = "dbo.spSuaHoaDon";
                SqlCommand cmd = new SqlCommand(strcmd, con);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add(new SqlParameter("@maHDB", maHD));
                cmd.Parameters.Add(new SqlParameter("@maKH", maKH));
                cmd.Parameters.Add(new SqlParameter("@maNH", maNH));
                cmd.Parameters.Add(new SqlParameter("@ngayLapHD", ngayLapHD));
                cmd.Parameters.Add(new SqlParameter("@thanhTien", thanhTien));
                SqlDataAdapter adapt = new SqlDataAdapter(cmd);
                DataTable dbtb = new DataTable();
                adapt.Fill(dbtb);
                con.Close();
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }

        public bool XoaHoaDon(int maHD)
        {
            try
            {
                con.Open();
                string strcmd = "dbo.spXoaHoaDon";
                SqlCommand cmd = new SqlCommand(strcmd, con);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add(new SqlParameter("@maHDB", maHD));
                SqlDataAdapter adapt = new SqlDataAdapter(cmd);
                DataTable dbtb = new DataTable();
                adapt.Fill(dbtb);
                con.Close();
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }

        public bool ThemHoaDon(int maKH, int maNH, DateTime ngayLap, int thanhTien)
        {
            try
            {
                con.Open();
                string strcmd = "dbo.spThemHoaDon";
                SqlCommand cmd = new SqlCommand(strcmd, con);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add(new SqlParameter("@maKH", maKH));
                cmd.Parameters.Add(new SqlParameter("@maNH", maNH));
                cmd.Parameters.Add(new SqlParameter("@ngayLapHD", ngayLap));
                cmd.Parameters.Add(new SqlParameter("@thanhTien", thanhTien));
                SqlDataAdapter adapt = new SqlDataAdapter(cmd);
                DataTable dbtb = new DataTable();
                adapt.Fill(dbtb);
                con.Close();
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }

        public bool ThemCTHoaDon(int maMonAn, int soLuong, int donGia)
        {
            try
            {
                con.Open();
                string strcmd = "dbo.spThemCTHoaDon";
                SqlCommand cmd = new SqlCommand(strcmd, con);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add(new SqlParameter("@maMonAn", maMonAn));
                cmd.Parameters.Add(new SqlParameter("@soLuong", soLuong));
                cmd.Parameters.Add(new SqlParameter("@donGia", donGia));
                SqlDataAdapter adapt = new SqlDataAdapter(cmd);
                DataTable dbtb = new DataTable();
                adapt.Fill(dbtb);
                con.Close();
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }
    }
}